//
//  UIBarButtonItem+Extension.swift
//  Weibo
//
//  Created by 黄亦寒 on 16/9/19.
//  Copyright © 2016年 HM. All rights reserved.
//

import UIKit
//给UIBarButtonItem扩展一个便利函数
extension UIBarButtonItem {
    //image:String? = nil:外界传入图片就设置图片，否则使用默认值nil
    convenience init(title:String,imageName:String? = nil, target:Any?,action:Selector) {
        self.init()
        
        let button = UIButton()
        
        if imageName != nil {
            button.setImage(UIImage(named:imageName!), for: .normal)
        }
        //添加点击事件
        button.addTarget(target, action: action, for: .touchUpInside)
        //设置文字
        button.setTitle(title, for: .normal)
        //设置文字颜色
        button.setTitleColor(UIColor.darkGray, for: .normal)
        button.setTitleColor(UIColor.orange, for: .highlighted)
        //设置文字大小
        button.titleLabel?.font = UIFont.systemFont(ofSize: 16)
        button.sizeToFit()
        
        customView = button
        
        
    }
}
